package cn.bby.tools.dialog.common;

import cn.bby.tools.component.FieldParseComponent;
import cn.bby.tools.state.StateReference;
import cn.bby.tools.utils.MessageUtil;
import com.intellij.openapi.project.Project;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.List;

/**
 * @description: BaseFieldParseDialog
 * @author: bby
 * @date: 2025/10/30 09:21
 * @version: 1.0
 */
public abstract class BaseFieldParseDialog extends BaseResultDisplayDialog implements StateReference {
    protected final FieldParseComponent sourceParseUi;

    @Getter
    private List<String> cols = Collections.emptyList();

    public BaseFieldParseDialog(@Nullable Project project, String title) {
        super(project, title);
        this.sourceParseUi = new FieldParseComponent("message.TargetText");
    }

    /**
     * 解析字符串为列
     *
     * @return 列名列表
     */
    public List<String> parseCols() {
        try {
            return sourceParseUi.parseCols();
        } catch (Exception e) {
            resultTextArea.setText(MessageUtil.getMessage("message.ParseError"));
            resultTextArea.setCaretPosition(0);
        }
        return Collections.emptyList();
    }

    @Override
    protected @NotNull Action @NotNull [] createActions() {
        // 创建自定义按钮：复制并关闭 和 关闭
        DialogWrapperAction copyAndCloseAction = new DialogWrapperAction(MessageUtil.getMessage("message.CopyAndClose")) {
            @Override
            protected void doAction(ActionEvent e) {
                saveState();
                doCopyAndCloseAction();
            }
        };

        DialogWrapperAction closeAction = new DialogWrapperAction(MessageUtil.getMessage("message.Close")) {
            @Override
            protected void doAction(ActionEvent e) {
                saveState();
                doCancelAction();
            }
        };

        return new Action[]{copyAndCloseAction, closeAction};
    }
    
    /**
     * 获取源文本
     * @return 源文本内容
     */
    public String getSourceText() {
        return sourceParseUi.getSourceText();
    }
    
    /**
     * 设置源文本
     * @param text 源文本内容
     */
    public void setSourceText(String text) {
        sourceParseUi.setSourceText(text);
    }
}